/**
 * 情况登记
 * @author truexin
 * @date   2016/06/01
 */

(function (lf, r) {

    var RC = r.createClass,
        ajax = lf.utils.ajax,
        urlOwner = '',
        urlMore = '',
        doAction = lf.bridgeJs.doAction;
    var houseId = '',
        houseNum = '',
        projectId = '',
        proprietorId = '',
        residentIdList = [],
        list = [],
        globalIdList = [];

    const RESIDENT = 'resident',
        STATUS = 'update',
        REGISTER = 'register',
        PROPRIETOR = 'proprietor';

    var Title = RC({
        render: function () {
            return (
                <div className="title">
                    <p>您还可以添加家人信息</p>
                </div>
            );
        }
    });

    var Panel = RC({
        getInitialState: function () {
            return ({
                houseId: '',
                list: [],
                houseNum: '',
                projectId: '',
                proprietorId: '',
                residentIdList: [],
                globalIdList: []
            })
        },
        getResidentList(houseId){
            let me = this;
            ajax({
                type: 'post',
                data: {
                    houseId: houseId,
                },
                success: function (data) {
                    if (data.dataList.length == 0) {
                        return
                    } else {
                        list = data.dataList.map(function (data) {
                            return (
                                <li className="li-active text-dot" key={data.id}>{data.name}</li>
                            )
                        });

                        $.each(data.dataList, function () {
                            residentIdList.push(this.id);
                            globalIdList.push(this.custGlobalId);
                        });
                        me.setState({
                            list: list,
                            residentIdList: residentIdList,
                            globalIdList: globalIdList,

                        });
                    }
                }.bind(me)
            }, lf.finger.api.household.residentList);
        },
        componentDidUpdate: function () {
            var list = r.findDOMNode(this.refs.list);
            houseId = this.state.houseId;
            houseNum = this.state.houseNum;

            var userList = $(list).find('.li-active'),
                me = this;
            $(userList).each(function (i) {
                $(this).on('tap', function () {
                    $(this).off();
                    var residentId = me.state.residentIdList[i];
                    var globalId = me.state.globalIdList[i];
                    doAction('property-register.html?type=' + RESIDENT + '&globalId=' + globalId + '&projectId=' + projectId + '&houseId=' + houseId + '&proprietorId=' + proprietorId + '&residentId=' + residentId + '&status=' + STATUS, '修改住户资料')
                });
            })
        },
        componentWillMount: function () {
            var me = this;
            let user = this.props.user;
            proprietorId = user.id;
            projectId = user.projectId;
            if (user.houseId) {
                ajax({
                    type: 'get',
                    data: {
                        houseId: user.houseId,
                    },
                    success: function (data) {
                        houseId = user.houseId;
                        houseNum = data.data.houseNum;

                        me.setState({
                            houseId: houseId,
                            houseNum: houseNum,

                            proprietorId: proprietorId,
                            projectId: projectId,
                        });
                    }.bind(me)
                }, lf.finger.api.household.getHouseInfo);
                me.getResidentList(user.houseId);
            } else {
                ajax({
                    type: 'get',
                    data: {
                        globalId: user.globalId,
                    },
                    success: function (data) {
                        var houseIdList = [],
                            houseNumList = [];
                        $.each(data.dataList, function () {
                            if (this.type == 1) {
                                houseIdList.push(this.houseId);
                                houseNumList.push(this.houseNum);
                            }
                        });
                        houseId = houseIdList[0];
                        houseNum = houseNumList[0];
                        me.setState({
                            houseId: houseId,
                            houseNum: houseNum,
                            proprietorId: proprietorId,
                            projectId: projectId,
                        });
                        me.getResidentList(houseId);

                    }.bind(me)
                }, lf.finger.api.household.getHouseList);
            }

        },
        componentDidMount: function () {
            var owner = document.getElementById('owner'),
                more = document.getElementById('more');

            $(owner).on('tap', function (e) {
                urlOwner = 'property-register.html?type=' + PROPRIETOR + '&projectId=' + projectId + '&houseId=' + houseId + '&proprietorId=' + proprietorId + '&houseNum=' + houseNum + '&status=' + STATUS;
                doAction(urlOwner,'修改业主资料')
            })

            $(more).on('tap', function (e) {
                urlMore = 'property-register.html?type=' + RESIDENT + '&projectId=' + projectId + '&houseId=' + houseId + '&proprietorId=' + proprietorId + '&status=' + REGISTER;
                doAction(urlMore, '登记住户资料')
            })
        },

        render: function () {

            return (
                <div className="panel">
                    <ul ref="list">
                        <li id="owner" className="text-dot" ref="me-profile" info={this.state}>我的资料</li>
                        {this.state.list}
                        <li id="more" ref="add" info={this.state}>添&nbsp;&nbsp;加</li>
                    </ul>
                </div>
            );
        }
    });

    var Body = RC({
        render: function () {
            return (
                <div className="property-register-info">
                    <Title/>
                    <Panel user={this.props.user}/>
                </div>
            );
        }
    });

    lf.bridgeJs.getUser(function (user) {
        proprietorId = user.id;
        projectId = user.projectId;

        lf.appJs.refresh();

        r.render(
            <Body user={user}/>,
            document.getElementById('root'), function () {
                $('#root').css('background', '#f4f4f4');
            }
        )
    })

})(liefeng, React)
